package cn.hy.batch;

import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.job.builder.FlowBuilder;
import org.springframework.batch.core.job.flow.Flow;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.SimpleAsyncTaskExecutor;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@Configuration
@Slf4j
public class SplitDemo {

    //注入任务对象工厂
    @Resource
    private JobBuilderFactory jobBuilderFactory;

    //任务的执行由Step决定,注入step对象的factory
    @Resource
    private StepBuilderFactory stepBuilderFactory;

    //创建Job对象
    //让flow2和flow3同时并发执行
    @Bean
    public Job jobDemo2() {
        return jobBuilderFactory.get("jobDemo2")
                .start(flow4()).split(new SimpleAsyncTaskExecutor())
                .add(flow5())
                .end()
                .build();
    }

    /**
     * 创建Flow对象，声明flow中包含哪些step
     *
     * @return
     */
    @Bean
    public Flow flow4() {
        return new FlowBuilder<Flow>("flow4").start(step7()).build();
    }

    /**
     * 创建Flow对象，声明flow中包含哪些step
     *
     * @return
     */
    @Bean
    public Flow flow5() {
        return new FlowBuilder<Flow>("flow5").start(step8()).next(step9()).build();
    }


    //创建Step对象
    @Bean
    public Step step7() {
        return stepBuilderFactory.get("step7").tasklet(new Tasklet() {
            @Override
            public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
                log.info("------step7 start");
                for (int i = 0; i < 3; i++) {
                    log.info("------step7 execute ..." + i);
                    TimeUnit.SECONDS.sleep(1);
                }
                log.info("------step7 end");
                return RepeatStatus.FINISHED;
            }
        }).build();
    }

    //创建Step对象
    @Bean
    public Step step8() {
        return stepBuilderFactory.get("step8").tasklet(new Tasklet() {
            @Override
            public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
                log.info("------step8 start");
                for (int i = 0; i < 3; i++) {
                    log.info("------step8 execute ..." + i);
                    TimeUnit.SECONDS.sleep(1);
                }
                log.info("------step8 end");
                return RepeatStatus.FINISHED;
            }
        }).build();
    }

    //创建Step对象
    @Bean
    public Step step9() {
        return stepBuilderFactory.get("step9").tasklet(new Tasklet() {
            @Override
            public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
                log.info("------step9 ok");
                return RepeatStatus.FINISHED;
            }
        }).build();
    }
}
